package com.zqh.userquery.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zqh.myfmmall.entity.Users;
import com.zqh.myfmmall.result.Result;
import com.zqh.userquery.dao.UsersQueryDao;
import com.zqh.userquery.service.UserQueryService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description :
 * @Author :kono淇杭哒、
 * @Create 2022-02-18 2:19
 */
@Service("userQueryService")
public class UserQueryServiceImpl extends ServiceImpl<UsersQueryDao, Users> implements UserQueryService{
    @Override
    public Result queryByUser(Users user) {
        Users targetUser = query().eq(user.getUsername() != null,"username", user.getUsername())
                .eq(user.getNickname() != null,"nickname",user.getNickname()).one();

        return Result.success(targetUser);
    }

    @Override
    public Result retrieveUserInfo(String userId) {
        Users user = query().eq("user_id", userId).select("user_id", "username", "password", "nickname", "realname", "user_img",
                "user_mobile", "user_email", "user_sex", "user_birth", "user_regtime", "user_modtime").one();
        return Result.success(user);
    }

    @Override
    public Result retrieveBatchUserInfo(List<String> ids) {
        //注意用in的话可能会导致顺序乱掉，因为查询的时候从数据库中第一条数据开始判断是否在in中
        /*List<Users> users = query().in("user_id", ids).select("user_id", "username", "password", "nickname", "realname", "user_img",
                "user_mobile", "user_email", "user_sex", "user_birth", "user_regtime", "user_modtime").list();*/
        ArrayList<Users> usersArrayList = new ArrayList<>();
        for (String id : ids) {
            Users user = query().eq("user_id", id).select("user_id", "username", "password", "nickname", "realname", "user_img",
                    "user_mobile", "user_email", "user_sex", "user_birth", "user_regtime", "user_modtime").one();
            usersArrayList.add(user);
        }
        return Result.success(usersArrayList);
    }

}
